#include <bits/stdc++.h>
// 2025/04/05
// tag: 
// Author: Forever
using namespace std;

class UF {
public:
    vector<int> parents;
    int cnt;

    UF(int n)
    {
        parents = vector<int>(n + 1);
        cnt = n;
        for (int i = 1; i <= n; i++)
            parents[i] = i;
    };

    int find(int i)
    {
        if (parents[i] != i)
            return parents[i] = find(parents[i]);
        return parents[i];
    }

    void un(int i, int j)
    {
        int ri = find(i), rj = find(j);
        if (ri == rj)
            return;
        parents[ri] = rj;
        cnt--;
    }

    int count()
    {
        return cnt;
    }

    bool connected(int p, int q)
    {
        return find(p) == find(q);
    }
};